package com.itheima.web.controller;

import com.itheima.domain.db.UserInfo;
import com.itheima.vo.MovementVo;
import com.itheima.vo.PageBeanVo;
import com.itheima.web.manager.AnalysisManager;
import com.itheima.web.manager.UserManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

//用户
@RestController
public class UserController {

    @Autowired
    private UserManager userManager;

    @Autowired
    private AnalysisManager analysisManager;

    //用户列表
    @GetMapping("/manage/users")
    public PageBeanVo findUserList(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {

        return userManager.findUserList(pageNum, pageSize);

    }

    //用户详情信息
    @GetMapping("/manage/users/{userID}")
    public UserInfo findUserInfo(@PathVariable("userID") Long userId) {
        return userManager.findUserInfo(userId);
    }

    //查询指定用户的视频列表
    @GetMapping("/manage/videos")
    public PageBeanVo findUserVideoList(Long uid,
                                        @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize
    ) {
        return userManager.findUserVideoList(uid, pageNum, pageSize);
    }

    //用户动态
    @GetMapping("/manage/messages")
    public PageBeanVo findUserMovementList(Long uid, String state,
                                           @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        //处理state
        Integer stateInt = null;
        if (StringUtils.isNotEmpty(state) && !StringUtils.equals("''", state)) {
            stateInt = Integer.parseInt(state);
        }

        return userManager.findUserMovementList(uid, stateInt, pageNum, pageSize);
    }

    //用户动态详情
    @GetMapping("/manage/messages/{id}")
    public MovementVo findMovementDetail(@PathVariable("id") String movementId) {
        return userManager.findMovementDetail(movementId);
    }

    //用户动态评论
    @GetMapping("/manage/messages/comments")
    public PageBeanVo findMovementCommentList(@RequestParam("messageID") String movementId,
                                              @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                              @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return userManager.findMovementCommentList(movementId, pageNum, pageSize);
    }

    @GetMapping("/dashboard/users")
    public Map summaryByTime(String type, Long sd, Long ed){
        return analysisManager.summaryByTime(type, sd, ed);
    }

    //审核通过
    @PostMapping("/manage/messages/pass")
    public String pass(@RequestBody String[] item){
        return userManager.pass(item);
    }

    //审核驳回
    @PostMapping("/manage/messages/reject")
    public String reject(@RequestBody String[] item){
        return userManager.reject(item);
    }

    //冻结用户
    @PostMapping("/manage/users/freeze")
    public String userFreeze(@RequestBody Map<String,Object> map){
        //获取后台冻结数据
        Integer userId = (Integer) map.get("userId");//用户id
        Integer freezingTime = Integer.parseInt((String) map.get("freezingTime")); //冻结时间
        Integer freezingRange = Integer.parseInt((String) map.get("freezingRange")); //冻结范围
        String reasonsForFreezing = (String) map.get("reasonsForFreezing");//冻结原因
        String frozenRemarks = (String) map.get("frozenRemarks"); //冻结备注
        //调用manager层
        userManager.userFreeze(userId,freezingTime,freezingRange,reasonsForFreezing,frozenRemarks);

        return "冻结用户！";

    }

    //解冻用户
    @PostMapping("/manage/users/unfreeze")
    public String unfreezeUser(@RequestBody Map<String,Object> map){
        //从前台获取参数
        Integer userId = (Integer) map.get("userId");
        String reasonsForThawing = (String) map.get("frozenRemarks");

        //调用manager方法
        userManager.unfreezeUser(userId,reasonsForThawing);
        return "解冻用户！";
    }


}
